package com.wzh.sending.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.wzh.sending.R;
import com.wzh.sending.info.UsersInfo;
import com.wzh.sending.view.RoundImageView;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/8/29.
 */
public class FindAdapter extends BaseAdapter {
    Context context;
    List<UsersInfo> usersInfos;
    double lat;
    double lot;
    DisplayImageOptions options;        // DisplayImageOptions是用于设置图片显示的类
    ImageLoader imageLoader;
    private ImageLoadingListener animateFirstListener;

    public FindAdapter(Context context, List<UsersInfo> usersInfos, ImageLoader imageLoader) {
        this.context = context;
        this.usersInfos = usersInfos;
        this.imageLoader = imageLoader;
        animateFirstListener = new AnimateFirstDisplayListener();
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.mipmap.logo_gray)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.logo_gray)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.logo_gray)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)                          // 设置下载的图片是否缓存在SD卡中
//                .displayer(new RoundedBitmapDisplayer(20))  // 设置成圆角图片
                .build();                                   // 创建配置过得DisplayImageOption对象

    }

    @Override
    public int getCount() {
        return usersInfos.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;

        if (view == null) {
            view = View.inflate(context, R.layout.item_find_user, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (TextUtils.isEmpty(usersInfos.get(i).getName())) {
            viewHolder.name.setVisibility(View.GONE);
        } else {
            viewHolder.name.setText(usersInfos.get(i).getName());
        }
        if (TextUtils.isEmpty(usersInfos.get(i).getLineNo())) {
            viewHolder.telNo.setText(usersInfos.get(i).getImeiNo());
        } else {
            viewHolder.telNo.setText(usersInfos.get(i).getLineNo());
        }
        double mi = Double.parseDouble(String.valueOf(AMapUtils.calculateLineDistance(new LatLng(lat, lot),
                new LatLng(usersInfos.get(i).getLat(), usersInfos.get(i).getLot()))));

        if (mi > 500) {
            viewHolder.juli.setText(turnMoney(mi / 1000) + "Km");
        } else {
            viewHolder.juli.setText(turnMoney(mi) + "m");
        }
        viewHolder.address.setText(usersInfos.get(i).getAddRess());
        viewHolder.time.setText("更新时间:" + usersInfos.get(i).getUpdatedAt());

        imageLoader.displayImage(usersInfos.get(i).getHeardUrl(), viewHolder.heard, options, animateFirstListener);
        return view;
    }

    /**
     * 图片加载第一次显示监听器
     *
     * @author Administrator
     */
    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                // 是否第一次显示
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    // 图片淡入效果
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

    public BigDecimal turnMoney(double t) {
        BigDecimal bd = new BigDecimal(t);
        bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);
        return bd;
    }

    public void setLotandLat(double lot, double lat) {
        this.lat = lat;
        this.lot = lot;
    }


    static class ViewHolder {
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.tel_no)
        TextView telNo;
        @Bind(R.id.juli)
        TextView juli;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.time)
        TextView time;

        @Bind(R.id.iv_headportrait)
        RoundImageView heard;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
